<?php
//ajax1.php
//Update Genus list when click "Click to Get Genera" button in Compendium search page
//Written by Hongjie Qu
//Last Updated: August 2009
?>
<?php


$debug=0;
$dbhost = "localhost";
$dbuser = "fossilplot";
$dbpass = "fossilplot";
$dbname = "fossilplot";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$Make = $_GET['Make'];
$arrClass = $_GET['arrClass'];
$arrOrder = $_GET['arrOrder'];

$s=mysql_query("select id from phylum where phylum_name='$Make'");
while ($row = mysql_fetch_array($s)) {
	$Makeid=$row{'id'};
}

if($debug){


echo "hello phylum".$Make."<br>";
echo "hello class ".$arrClass."<br>";
echo "hello order ".$arrOrder."<br>";
echo "size of class is ".sizeof($arrClass)."<br>";
}



$exploded_class = explode(',', $arrClass);
if($debug){
echo "exploded class 3 is ".$exploded_class[3]."<br>";
echo "size of class is ".sizeof($exploded_class)."<br>";
}
$clause_class = "(";
foreach($exploded_class as $tmp_class) {
$result = mysql_query("select id from class where class_name='$tmp_class'");
while ($row = mysql_fetch_array($result)) {
if($clause_class=="("){
   $clause_class = $clause_class.$row{'id'};
}
else{$clause_class = $clause_class.",".$row{'id'};}

}

if($debug){
print "tmp class is ".$tmp_class;
print "<br>";
}
}

$clause_class= $clause_class.")";
if($debug){
print "clause class is ".$clause_class."<br>";
}



$exploded_order = explode(',', $arrOrder);
$k=0; //trace empty order
if($debug){
echo "exploded order 3 is ".$exploded_order[3]."<br>";
echo "size of order is ".sizeof($exploded_order)."<br>";
}
$clause_order = "(";
foreach($exploded_order as $tmp_order) {
if(strpos($tmp_order, "No related order to")) $k=1;
$result1 = mysql_query("select id from fossilplot.order where order_name='$tmp_order'");
while ($row1 = mysql_fetch_array($result1)) {
if($clause_order=="("){
   $clause_order = $clause_order.$row1{'id'};
}
else{$clause_order = $clause_order.",".$row1{'id'};}

}

if($debug){
print "tmp order is ".$tmp_order;
print "<br>";
}
}
if($k==1){
	if($clause_order=="(") $clause_order=$clause_order."0";
	else	$clause_order=$clause_order.","."0";//in genus, empty order id is 0
}
$clause_order= $clause_order.")";
if($debug){
print "clause order is ".$clause_order."<br>";
}

if($clause_class != "()"){
	if($clause_order!="()"){
		$rt=mysql_query("SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and class_id in $clause_class and order_id in $clause_order");
		$bg= "SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and class_id in $clause_class and order_id in $clause_order";
	}
	else{
		$rt=mysql_query("SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and class_id in $clause_class");
		$bg="SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and class_id in $clause_class";
	}
}
else{
	if($clause_order != "()"){
		$rt=mysql_query("SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and order_id in $clause_order");
		$bg="SELECT * FROM fossilplot.genus where phylum_id='$Makeid' and order_id in $clause_order";
	}
	else{
		$rt=mysql_query("SELECT * FROM fossilplot.genus where phylum_id='$Makeid'");
		$bg="SELECT * FROM fossilplot.genus where phylum_id='$Makeid'";
	}
}

//echo "bg is ".$bg."<br>";
echo "<select name=\"F\" multiple size=\"15\" style=\"width: 215px;\">";

while($rowgen = mysql_fetch_array($rt)){

	echo "<option>".$rowgen{'id'}." ".$rowgen{'genus_name'}."</option>";
}
echo "</select>";
?>